/*
 * @Author: yanjikun
 * @Date: 2021-05-28 13:53:25
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-06-13 14:20:30
 * @Description: file content
 */
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createStyleImportPlugin, AntdResolve } from 'vite-plugin-style-import'
import { viteMockServe } from 'vite-plugin-mock'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { VueAmapResolver } from '@vuemap/unplugin-resolver'
import vueJsx from '@vitejs/plugin-vue-jsx'
import DefineOptions from 'unplugin-vue-define-options/vite'
import compressPlugin from 'vite-plugin-compression'
import WindiCSS from 'vite-plugin-windicss'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const root = process.cwd()
  const { VITE_APP_PUBLIC_PATH, VITE_APP_BASE_URL } = loadEnv(mode, root)
  const isBuild = command === 'build'
  return {
    base: VITE_APP_PUBLIC_PATH,
    resolve: {
      alias: {
        '@': path.resolve(__dirname, '.', 'src'),
      },
      mainFields: ['module'],
    },
    plugins: [
      vue(),
      DefineOptions(),
      vueJsx(),
      viteMockServe({
        ignore: /^_/,
        mockPath: 'mock',
        localEnabled: !isBuild,
        prodEnabled: isBuild,
        logger: true,
      }),
      WindiCSS(),
      createStyleImportPlugin({
        resolves: [AntdResolve()],
        libs: [
          {
            libraryName: 'ant-design-vue',
            esModule: true,
            resolveStyle: (name) => {
              return `ant-design-vue/es/${name}/style/index`
            },
          },
        ],
      }),
      createSvgIconsPlugin({
        // 配置路劲在你的src里的svg存放文件
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      AutoImport({
        resolvers: [VueAmapResolver()],
      }),
      Components({
        resolvers: [VueAmapResolver()],
      }),
      compressPlugin({
        ext: '.gz',
        algorithm: 'gzip',
        deleteOriginFile: false,
      }),
    ],
    server: {
      proxy: {
        '/file': {
          target: VITE_APP_BASE_URL,
          secure: true,
          changeOrigin: true,
          // rewrite: path => path.replace(/^\/api/, "")
        },
      },
    },
    build: {
      rollupOptions: {
        output: {
          /**分割打包 */
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString()
            }
          },
        },
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          modifyVars: {},
          additionalData: `
            @import "ant-design-vue/lib/style/themes/default.less";
          `,
        },
      },
    },
    esbuild: {
      pure: ['console.log', 'debugger'],
    },
  }
})
